package com.bamboo.tool.template.torna.util;

import cn.hutool.core.collection.CollUtil;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.template.torna.entity.*;
import com.bamboo.tool.view.component.notificationGroup.ToolWindowNotificationGroup;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import lombok.SneakyThrows;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bamboo
 * Date 2022/9/21 10:35
 * Description
 */
public class TornaUtil {

    /**
     * 刷新模块
     */
    @SneakyThrows
    public static void refreshModule(Project myproject) {

        ApplicationManager.getApplication().runReadAction(() -> {
            TornaSetting state = RestfulCloudApplicationCache.getInstance().getState().getTornaSetting();
            state.getSpaces().clear();
            state.getProjects().clear();
            state.getModules().clear();
            TornaRespBase<List<SpaceDTO>> spaceList = TornaRequestUtil.getSpaceList();
            if (!spaceList.isOk()) {
                ToolWindowNotificationGroup.NOTIFICATION_GROUP.createNotification("Reload Torna error:" + spaceList.getMsg(), MessageType.INFO).notify(myproject);
            }
            List<SpaceDTO> spaces = spaceList.getData();
            if (CollUtil.isEmpty(spaces)) {
                return;
            }
            state.getSpaces().addAll(spaces);
            for (SpaceDTO space : spaces) {
                TornaRespBase<List<ProjectDTO>> spaceProjectList = TornaRequestUtil.getSpaceProjectList(space.getId());
                if (!spaceProjectList.isOk()) {
                    ToolWindowNotificationGroup.NOTIFICATION_GROUP.createNotification("Reload Torna error:" + spaceList.getMsg(), MessageType.ERROR).notify(myproject);
                }
                List<ProjectDTO> projects = spaceProjectList.getData();
                if (CollUtil.isEmpty(projects)) {
                    continue;
                }
                Map<String, List<ProjectDTO>> projectMap = projects.stream().collect(Collectors.groupingBy(e -> e.getSpaceId(), Collectors.toList()));
                state.getProjects().putAll(projectMap);
                for (ProjectDTO project : projects) {
                    TornaRespBase<List<ModuleVO>> projectModelList = TornaRequestUtil.getProjectModelList(project.getId());
                    if (!projectModelList.isOk()) {
                        ToolWindowNotificationGroup.NOTIFICATION_GROUP.createNotification("Reload Torna error:" + spaceList.getMsg(), MessageType.ERROR).notify(myproject);
                    }
                    List<ModuleVO> modules = projectModelList.getData();
                    if (CollUtil.isEmpty(modules)) {
                        return;
                    }
                    Map<String, List<ModuleVO>> moduleMap = modules.stream().collect(Collectors.groupingBy(e -> e.getProjectId(), Collectors.toList()));
                    state.getModules().putAll(moduleMap);
                }
            }
            ToolWindowNotificationGroup.NOTIFICATION_GROUP.createNotification("Reload Torna Success", MessageType.INFO).notify(myproject);
        });

    }


}
